import request from '@/config/axios'
import type { AdminData } from './types'
import { Md5 } from "ts-md5";

export const getAdminListApi = (params: any): Promise<IResponse> => {
    const queryParams = params.queryParams ? params.queryParams : {}
    if (params.queryParams) {
        delete params.queryParams
    }
    Object.assign(params, queryParams)
    return request.get({url: '/admin/list', params})
}

export const saveOrUpdate = (data: Partial<AdminData>, isUpdate = false): Promise<IResponse> => {
    if (isUpdate) {
        return request.put({url: '/admin/', data})
    } else {
        return request.post({url: '/admin/', data})
    }
}

export const getAdminDetailApi = (id: string | number): Promise<IResponse<AdminData>> => {
    return request.get({url: `/admin/${id}` })
}

export const delAdminApi = (data: Partial<AdminData>[]): Promise<IResponse> => {
    if (data.length > 0) {
        const ids = data.map((d) => d.id);
        return request.delete({ url: `/admin/delete/${ids.join(',')}` })
    } else {
        const id = data[0].id
        return request.delete({ url: `/admin/${id}` })
    }
}
// 登录
export const login = (data: {username: string, password: string}): Promise<IResponse> => {
    data.password = Md5.hashStr(data.password).toUpperCase();
    return request.post({ url: `/admin/login` , data})
}
